﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;
namespace WEBDT.Areas.Admin.Controllers
{
    public class LoaiSPController : BaseController<LoaiSP>
    {
        //
        // GET: /Admin/LoáiP/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public override ActionResult insert()
        {
            var model = new LoaiSPModel
            {
                tontai = false,
            };
            //    ViewData["NhomSP"] = new SelectList(repository.DanhsachNhom(), "MaNhom", "TenNhom");
            ViewData["NhomSP"] = new SelectList(repository.Danhsach<NhomSP>(), "MaNhom", "TenNhom");
            return View(model);
        }

        [HttpPost]
        public override ActionResult insert(FormCollection fr)
        {
            var httt = new LoaiSPModel
            {
                TenLoai = fr.Get("tenloai"),
                MaNhom = Convert.ToInt32(fr.Get("MaNhom")),
                tontai = false
            };

            var model = new LoaiSP
            {
                TenLoai = httt.TenLoai,
                MaNhom = httt.MaNhom,
            };
            repository.Insert<LoaiSP>(model);
            return RedirectToAction("getAll");
        }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var ht = repository.SelectByID<LoaiSP>(id);

            var model = new LoaiSPModel
            {
                MaLoai = ht.MaLoai,
                MaNhom = ht.MaNhom,
                TenLoai = ht.TenLoai,
                tontai = false,
                kiemtra = ht.TenLoai,
            };

            ViewData["NhomSP"] = new SelectList(repository.Danhsach<NhomSP>(), "MaNhom", "TenNhom");
            return View(model);
        }
        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var ht = new LoaiSPModel
            {
                MaLoai = Convert.ToInt32(fr.Get("MaLoai")),
                TenLoai = fr.Get("tenloai"),
                MaNhom = Convert.ToInt32(fr.Get("MaNhom")),
                tontai = false
            };


            var model = repository.SelectByID<LoaiSP>(ht.MaLoai);
            model.TenLoai = ht.TenLoai;
            model.MaNhom = ht.MaNhom;
            repository.commitchanges();
            return RedirectToAction("getAll");
        }
    }
}
